package com.android.support.lib.universal.cart;

import com.android.support.lib.universal.R;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * 购物车加减文本框
 *
 * @author 彭治铭
 */
public class AddAndSubView extends LinearLayout {
    public Button a;// 加
    public Button b;// 减
    public EditText c;// 文本框
    public Listennersub sub;// 回调函数，当前数值发生改变时回调。
    public int minNum = 1;// 最小值
    public int maxNum = 100;// 最大值
    public int num = minNum;// 当前值
    public int price = 1;// 单价
    public int total = 1;// 总价
    private Context e;
    private LinearLayout f;
    private LinearLayout g;
    private LinearLayout h;
    private LinearLayout i;
    private int k;
    private int l;
    private int m;
    private int n;
    private int o;
    private int p;

    public AddAndSubView(Context paramContext) {
        super(paramContext);
        this.e = paramContext;
        a();
    }

    public AddAndSubView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        this.e = paramContext;
        a();
    }

    private int a(Context paramContext, float paramFloat) {
        return (int) (0.5F + paramFloat
                * paramContext.getResources().getDisplayMetrics().density);
    }

    /**
     * 获取数量
     *
     * @return
     */
    public int getNum() {
        if (this.c.getText().toString() != null
                && this.c.getText().toString().trim().length() > 0) {
            return Integer.parseInt(this.c.getText().toString());
        }
        return num;
    }

    /**
     * 设置数量
     *
     * @param paramInt
     * @param bool     是否需要更新文本框
     */
    public void setNum(int paramInt, Boolean bool) {
        if (paramInt >= maxNum) {
            paramInt = maxNum;
            Toast.makeText(e, "亲，已经是最大值了", Toast.LENGTH_LONG).show();
        }
        if (paramInt <= minNum) {
            paramInt = minNum;
            Toast.makeText(e, "亲，已经是最小值了", Toast.LENGTH_LONG).show();
        }
        this.num = paramInt;
        if (bool) {
            this.c.setText(String.valueOf(paramInt));
        }
        this.total = this.num * this.price;
        if (sub != null) {
            // 回调函数
            sub.results(paramInt, price, total);
        }
    }

    /**
     * 添加回调函数，当前数值发生改变时回调。
     *
     * @param parama
     */
    public void setOnNumChangeListener(Listennersub parama) {
        this.sub = parama;
    }

    // 监听事件
    private void setViewListener() {

        OnClickListener paramOnClickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int num = getNum();

                if (v.getTag().equals("+")) {
                    num++;
                    setNum(num, true);
                }
                if (v.getTag().equals("-")) {
                    num--;
                    setNum(num, true);

                }
            }
        };
        this.a.setOnClickListener(paramOnClickListener);
        this.b.setOnClickListener(paramOnClickListener);
        this.c.addTextChangedListener(new TextWatcher() {
            private Integer chanNum;// 获取文本实时变化的内
            private Integer beforNum = num;// 记录当前的数值

            /**
             * s 文本框里的所有内容（包括当前正在输入的文字） start 文字开始输入的位置（文字输入之前，光标的位置(从0开始)）
             * start 和 before 是一样的 before 文字开始输入的位置（文字输入之前，光标的位置(从0开始)） count
             * 文字总数
             */
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                // TODO Auto-generated method stub
                if (s != null && s.toString().trim().length() > 0
                        && !s.toString().trim().equals("")) {
                    chanNum = Integer.valueOf(s.toString());
                    if (chanNum < minNum) {
                        c.setText(String.valueOf(minNum));// 设置文本最小值
                    }
                    if (chanNum > maxNum) {
                        c.setText(String.valueOf(maxNum));// 设置文本最大值
                    }
                    if (s.toString().length() != chanNum.toString().length()) {
                        c.setText(String.valueOf(chanNum));// 不允許出現0开头的数值(001,010等)
                    } else {
                        setNum(chanNum, false);
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub
                beforNum = num;// 记录当前的数值
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (s != null && s.toString().trim().length() > 0
                        && !s.toString().trim().equals("")) {
                } else {
                    num = beforNum;
                }
            }
        });
    }

    /**
     * 总流程
     */
    private void a() {
        b();
        c();
        d();
        f();
        setViewListener();// 添加监听事件
    }

    /**
     * 设置文本框
     */
    private void c() {
        this.f = new LinearLayout(this.e);
        this.g = new LinearLayout(this.e);
        this.h = new LinearLayout(this.e);
        this.i = new LinearLayout(this.e);
        this.a = new Button(this.e);
        this.b = new Button(this.e);
        this.c = new EditText(this.e);
        this.a.setTag("+");
        this.b.setTag("-");
        this.c.setInputType(2);// 设定为数值输入类型
        this.c.setText(String.valueOf(this.minNum));// 设置文本默认值
        this.c.setTextColor(Color.parseColor("#000000"));//黑色
        this.c.setPadding(0, 0, 0, 0);
        this.c.setCursorVisible(false);// 不显示光标
    }

    /**
     * 设置样式
     */
    private void d() {
        LayoutParams localLayoutParams = new LayoutParams(
                -1, -1);
        this.a.setLayoutParams(localLayoutParams);
        this.a.setBackgroundResource(R.drawable.lib_btn_submit_add);// 加
        this.b.setLayoutParams(localLayoutParams);
        this.b.setBackgroundResource(R.drawable.lib_btn_submit_sub);// 减
        this.c.setLayoutParams(localLayoutParams);
        this.c.setGravity(17);
        this.c.setBackgroundResource(R.drawable.lib_btn_submit_input);// 文本框
        e();
        localLayoutParams.gravity = Gravity.CENTER;
        this.h.setLayoutParams(localLayoutParams);
        this.h.setFocusable(true);
        this.h.setFocusableInTouchMode(true);
        localLayoutParams.width = -2;
        localLayoutParams.weight = 1.0F;
        this.g.setLayoutParams(localLayoutParams);
        this.i.setLayoutParams(localLayoutParams);
        a(30, 30);
        localLayoutParams.width = -1;
        this.f.setLayoutParams(localLayoutParams);
        this.f.setOrientation(LinearLayout.HORIZONTAL);
    }

    private void b() {
        this.k = -1;
        this.l = -1;
        this.m = -1;
        this.n = -1;
        this.o = -1;
        this.p = -1;
    }

    public void setTextSize(int paramInt) {
        this.c.setTextSize(paramInt);
    }

    public void setEditTextHeight(int paramInt) {
        this.p = paramInt;
        e();
    }

    public void setEditTextLayoutHeight(int paramInt) {
        this.l = paramInt;
        e();
    }

    public void setEditTextLayoutWidth(int paramInt) {
        this.k = paramInt;
        e();
    }

    public void setEditTextMinHeight(int paramInt) {
        if (paramInt > 0) {
            this.o = paramInt;
            this.c.setMinHeight(a(this.e, paramInt));
        }
    }

    public void setEditTextMinimumHeight(int paramInt) {
        if (paramInt > 0) {
            this.n = paramInt;
            this.c.setMinimumHeight(a(this.e, paramInt));
        }
    }

    public void setEditTextMinimumWidth(int paramInt) {
        if (paramInt > 0) {
            this.m = paramInt;
            this.c.setMinimumWidth(a(this.e, paramInt));
        }
    }

    private void e() {
        if (this.m < 0)
            this.m = a(this.e, 100.0F);
        this.c.setMinimumWidth(a(this.e, this.m));
        this.h.setMinimumWidth(a(this.e, this.m));
        if (this.p > 0) {
            if ((this.o >= 0) && (this.o > this.p))
                this.p = this.o;
            this.c.setHeight(a(this.e, this.p));
        }
        if (this.l > 0) {
            if ((this.n > 0) && (this.n > this.l))
                this.l = this.n;
            LayoutParams localLayoutParams2 = (LayoutParams) this.c
                    .getLayoutParams();
            localLayoutParams2.height = a(this.e, this.l);
            this.c.setLayoutParams(localLayoutParams2);
            this.h.setLayoutParams(localLayoutParams2);
        }
        if (this.k > 0) {
            if ((this.m > 0) && (this.m > this.k))
                this.k = this.m;
            LayoutParams localLayoutParams1 = (LayoutParams) this.c
                    .getLayoutParams();
            localLayoutParams1.width = a(this.e, this.k);
            this.c.setLayoutParams(localLayoutParams1);
            this.h.setLayoutParams(localLayoutParams1);
        }
        this.c.setTextSize(14.0F);
    }

    private void f() {
        this.f.addView(this.g, 0);
        this.f.addView(this.h, 1);
        this.f.addView(this.i, 2);
        this.g.addView(this.b);
        this.h.addView(this.c);
        this.i.addView(this.a);
        addView(this.f);
    }

    public void a(int paramInt1, int paramInt2) {
        LayoutParams localLayoutParams = new LayoutParams(
                a(this.e, paramInt1), a(this.e, paramInt2));
        this.a.setLayoutParams(localLayoutParams);
        this.b.setLayoutParams(localLayoutParams);
        this.g.setLayoutParams(localLayoutParams);
        this.i.setLayoutParams(localLayoutParams);
    }
}